package com.moleq.device;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.moleq.device.printer.Printer;


public class DeviceManager {

	private static DeviceManager instance;

	public static DeviceManager getInstance() {
		if (instance == null) {
			instance = new DeviceManager();

		}
		return instance;
	}

	public Printer ReceiptPrinter;
	public HashMap<Integer,Printer> ServiceTicketPrinters;
	public IPrinter printer;
	public IScale scale;
	public BasePinPad pinPad;
//	public IScanner scanner;
	public ILineScreen screen;
	public List<IScanner> scanners;
	public List<IScale> scales;
	public boolean scannerIsReady = false;
	public boolean scaleIsReady = false;
	public boolean pinPadIsReady = false;
	
	public String strText;
	private IDeviceEvent mIDeviceEvent;
	private List<ScannerSetting> mScannerSettings;
	private ArrayList<IPinPad> posMachines;
	
	public  DeviceManager(){
		scanners = new ArrayList<IScanner>();
		scales = new ArrayList<IScale>();
	}
	
	public void setReceiptPrinter(Printer printer){
		ReceiptPrinter = printer;
	}

	public void setPrinter(IPrinter basePrinter) {
		printer = basePrinter;

	}

	public void setScale(IScale baseScale) {
		scale = baseScale;
		scaleIsReady = true;
	}
	
	public void setPinPad(BasePinPad pos){
		pinPad = pos;
	}

//	public void setScanner(IScanner baseScanner) {
//		scanner = baseScanner;
//
//	}

	public void setScreen(ILineScreen baseScreen) {
		screen = baseScreen;

	}

	public void setString(String string) {
		this.strText = string;
		
	}

	public void addScanner(IScanner scanner) {
		if (scanners == null || scanners.isEmpty()) {
			scanners = new ArrayList<IScanner>();
		}
		scanners.add(scanner);
		this.scannerIsReady = true;
	}

	public void addScale(IScale scale) {
		if (scales == null || scales.isEmpty()) {
			scales = new ArrayList<IScale>();
		}
		scales.add(scale);
	}
	
	public void addPosMachine(IPinPad pos){
		if (posMachines == null || posMachines.isEmpty()) {
			posMachines = new ArrayList<IPinPad>();
		}
		posMachines.add(pos);
	}

	public void resetALLDevices() {
		scanners = new ArrayList<IScanner>();
		scales = new ArrayList<IScale>();
		posMachines = new  ArrayList<IPinPad>();

		scale = null;
		printer = null;
		screen = null;
		pinPad = null;
		ReceiptPrinter = null;
		
		this.scaleIsReady = false;
		this.scannerIsReady = false;
		this.pinPadIsReady = false;

	}
	
	public void setDeviceCallbackEvent(IDeviceEvent event) {
		this.mIDeviceEvent = event;
	}

	public void setScannerSettingLists(List<ScannerSetting> settings) {
		this.mScannerSettings = settings;
	}

	public void buildAllScanner() {
		for (int i = 0; i < mScannerSettings.size(); i++) {
			BaseScanner baseScanner = new BaseScanner();
			baseScanner.setScannerSetting(mScannerSettings.get(i));
			this.addScanner(baseScanner);
		}
	}
	
	public void connectAllScanner() {
		int count =  scanners.size();
		for (int i = 0; i < count; i++) {
			scanners.get(i).onConnect();
			scanners.get(i).setCallbackEvent(mIDeviceEvent);
		}
	}

	public void disconnectAllScanner() {
		int count =  scanners.size();
		for (int i = 0; i < count; i++) {
			scanners.get(i).onDisconnect();
			scanners.get(i).setCallbackEvent(null);
		}
	}

	public void enableAllScanner() {
		int count =  scanners.size();
		for (int i = 0; i < count; i++) {
			scanners.get(i).onEnableScanner();
		}
	}

	public void disableAllScanner() {
		int count =  scanners.size();
		for (int i = 0; i < count; i++) {
			scanners.get(i).onDisableScanner();
		}

	}
	
	public void clearAllScannerBuffer() {
		int count =  scanners.size();
		for (int i = 0; i < count; i++) {
			scanners.get(i).onClearBuffer();
		}

	}

	public void disposeScale() {
		if(scale != null){
			scale.onDisableScale();
			scale.onDisconnect();
		}
		 
		
	}

	public void disposeScanner() {
		disableAllScanner();
		disconnectAllScanner();
		
	}
	
	public void setDisplayMessage(String msg){
		if (screen !=null) {
			screen.displayLine(msg);
		}
	}
	
	public void disposePrinter(){
		Map<Integer,Printer> map = new HashMap(); 
		Iterator it = map.entrySet().iterator(); 
		while (it.hasNext()) { 
			Map.Entry entry = (Map.Entry) it.next(); 
			Object key = entry.getKey(); 
			Printer value = (Printer) entry.getValue(); 
			value.close();
		} 
	}
}
